/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.assistant.common.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.assistant.common.utils.WebUtils;

public class ResourcePathTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getLog(ResourcePathTag.class);

	private String base = null;// 输出javascript版web root，并在页面上输出<base href=""/>标签
	private String css = null;// 一般资源
	private String js = null;// javascript脚本
	private String sysRes = null;// 系统预定义的资源

	private static List<String> cssList = new ArrayList<String>();
	private static List<String> jsList = new ArrayList<String>();

	private static String cssPrefix = "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
	private static String cssSuffix = "\">";
	private static String scriptPrefix = "<script type=\"text/javascript\" src=\"";
	private static String scriptSuffix = "\"></script>";

	static {
		cssList.add("/res/scripts/yui/reset-fonts-grids/reset-fonts-grids.css");
		cssList.add("/res/scripts/yui/layout/assets/layout-core.css");

		jsList.add("/res/scripts/yui/yahoo-dom-event/yahoo-dom-event.js");
		jsList.add("/res/scripts/yui/element/element-min.js");
		jsList.add("/res/scripts/yui/selector/selector-min.js");
		jsList.add("/res/scripts/yui/layout/layout-min.js");
	}

	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();

		// the full webapp path (http://ip:port/contextPath)
		// Note: there is no "/" at the end of path
		String webPath = WebUtils.getWebRoot(req);

		if (getCss() != null) {// 处理CSS资源等
			processCss(out, webPath);
		} else if (getJs() != null) {// 处理javascript资源
			processJs(out, webPath);
		} else if (getBase() != null) {// 输出用于标记根路径的javascript变量
			processBase(out, webPath);
		} else if (getSysRes() != null) {// 处理系统预定义资源
			processSysRes(out, webPath);
		} else {
			log.info("Must surpply at least one key!");
		}
		return (SKIP_BODY);
	}

	/**
	 * 处理CSS资源等
	 * 
	 * @param out
	 * @param webPath
	 */
	protected void processCss(JspWriter out, String webPath) {
		try {
			String resourceUrl = webPath + getCss();
			out.print(cssPrefix + resourceUrl + cssSuffix);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理javascript资源
	 * 
	 * @param out
	 * @param webPath
	 */
	protected void processJs(JspWriter out, String webPath) {
		try {
			String resourceUrl = webPath + getJs();
			out.print(scriptPrefix + resourceUrl + scriptSuffix);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 输出用于标记根路径的javascript变量
	 * 
	 * @param out
	 * @param webPath
	 */
	protected void processBase(JspWriter out, String webPath) {
		try {
			//输出base标签
			out.print("<base href=\"");
			out.print(webPath);
			out.print("/\"");
			out.print("></base>");
			
			out.print("<script type=\"text/javascript\">");
			out.print("var " + getBase() + " = \"" + webPath + "\";");
			out.print("</script>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理系统预定义资源
	 * 
	 * @param out
	 * @param webPath
	 */
	protected void processSysRes(JspWriter out, String webPath) {
		try {
			if ("base.res".equals(getSysRes())) {// 基本资源
				// 在页面上输出CSS
				for (String css : cssList) {
					out.print(cssPrefix + webPath + css + cssSuffix);
				}
				// 在页面上输出JavaScript
				for (String js : jsList) {
					out.print(scriptPrefix + webPath + js + scriptSuffix);
				}
			} else if ("layout.res".equals(getSysRes())) {// 部署资源
				out
					.print(cssPrefix
							+ webPath
							+ "/res/scripts/yui/reset-fonts-grids/reset-fonts-grids.css"
							+ cssSuffix);
				out.print(cssPrefix + webPath
						+ "/res/scripts/yui/layout/assets/layout-core.css"
						+ cssSuffix);

				out.print(scriptPrefix + webPath
						+ "/res/scripts/yui/yahoo-dom-event/yahoo-dom-event.js"
						+ scriptSuffix);
				out.print(scriptPrefix + webPath
						+ "/res/scripts/yui/element/element-min.js"
						+ scriptSuffix);
				out.print(scriptPrefix + webPath
						+ "/res/scripts/yui/selector/selector-min.js"
						+ scriptSuffix);
				out.print(scriptPrefix + webPath
						+ "/res/scripts/yui/layout/layout-min.js"
						+ scriptSuffix);
			} else if ("columntree.res".equals(getSysRes())) {// 扩展资源
				out
					.print(cssPrefix + webPath
							+ "/res/themes/default/css/column-tree.css"
							+ cssSuffix);

				out.print(scriptPrefix + webPath
						+ "/res/scripts/ext/ColumnNodeUI.js" + scriptSuffix);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getSysRes() {
		return sysRes;
	}

	public void setSysRes(String sysRes) {
		this.sysRes = sysRes;
	}

	public String getJs() {
		return js;
	}

	public void setJs(String js) {
		this.js = js;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}
}
